//
//  CHTabBarController.m
//  SZTZ
//
//  Created by 陈浩 on 2017/3/6.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHTabBarController.h"
#import "CHHomeViewController.h"
#import "CHInvestmentViewController.h"
#import "CHKeyIndustryViewController.h"
#import "CHTrueStoryViewController.h"
#import "CHMajorProjectViewController.h"
#import "CHNavigationController.h"
#import "UITabBar+badge.h"
#import "CHGYTZViewController.h"
@interface CHTabBarController ()

@end

@implementation CHTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
  
    self.tabBar.backgroundColor = [UIColor whiteColor];
    
    //添加子控制器
    [self setupChildContrs];

    
    
    BOOL islogin = [Util boolForkey:@"isLogin"];
    if (!islogin) {
        //显示
        [self.tabBar showBadgeOnItemIndex:1];
        [self.tabBar showBadgeOnItemIndex:2];
        [self.tabBar showBadgeOnItemIndex:3];
        [self.tabBar showBadgeOnItemIndex:4];
    }else{
        //    是否有新数据
        [self checkNewData];
    }
}

-(void)setupChildContrs{
    
    CHHomeViewController *homeVC = [[CHHomeViewController alloc] init];
    [self addChildVC:homeVC title:@"全社会固投" imgName:@"nav01"];
    
    
    CHInvestmentViewController *investmentVC = [[CHInvestmentViewController alloc] init];
    [self addChildVC:investmentVC title:@"政府投资" imgName:@"nav02"];
    
    
    
    CHMajorProjectViewController *majorVC = [[CHMajorProjectViewController alloc] init];
    [self addChildVC:majorVC title:@"重大项目" imgName:@"nav03"];
    
    
//    CHTrueStoryViewController *trueStoryVC = [[CHTrueStoryViewController alloc] init];
//    [self addChildVC:trueStoryVC title:@"民生工程" imgName:@"nav04"];
    
    
    
    CHKeyIndustryViewController *keyIndustryVC = [[CHKeyIndustryViewController alloc] init];
    [self addChildVC:keyIndustryVC title:@"重点区域" imgName:@"nav05"];
    
    
    CHGYTZViewController *gytzVC = [[CHGYTZViewController alloc] init];
    [self addChildVC:gytzVC title:@"工业投资" imgName:@"nav06"];
}



-(void)addChildVC:(UIViewController *)vc title:(NSString *)title imgName:(NSString *)imgName{
    //    设置UITabbarButton 的图片和文字
    CHNavigationController *nav = [[CHNavigationController alloc] initWithRootViewController:vc];
    vc.title = title;
    vc.view.backgroundColor = [UIColor whiteColor];
    nav.tabBarItem.title = title;
    nav.tabBarItem.image = [UIImage imageNamed:imgName];
    NSString *selectedImgName = [imgName stringByAppendingString:@"_selected"];
    UIImage *selectedImg = [UIImage imageNamed:selectedImgName];
    
    //     设置图片的一个渲染的方式，返回一个没有渲染效果的图片
    selectedImg = [selectedImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav.tabBarItem.selectedImage = selectedImg;
    
    [self addChildViewController:nav];
}


-(void)checkNewData{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"username"] = [Util getValue:@"username"];
    [[Globle getInstance].service RequestWithServiceName:@"fgwoa_checkVersion" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
//        NSLog(@" 是否有新的数据 %@",result);
        
        for (NSString *str in result) {
            
            if ([str isEqualToString:@"政府投资"]) {
                
                if ([result[str] isEqual:@"1"]) {
                    //显示
                    [self.tabBar showBadgeOnItemIndex:1];
                }
            }else if ([str isEqualToString:@"重大项目"]) {
                if ([result[str] isEqual:@"1"]) {
                    //显示
                    [self.tabBar showBadgeOnItemIndex:2];
                }
//            }else if ([str isEqualToString:@"民生工程"]) {
//                if ([result[str] isEqual:@"1"]) {
//                    //显示
//                    [self.tabBar showBadgeOnItemIndex:3];
//                }
            }else if ([str isEqualToString:@"重点区域"]) {
                if ([result[str] isEqual:@"1"]) {
                    //显示
                    [self.tabBar showBadgeOnItemIndex:3];
                }
            }else if ([str isEqualToString:@"重点区域"]) {
                if ([result[str] isEqual:@"1"]) {
                    //显示
                    [self.tabBar showBadgeOnItemIndex:4];
                }
            }

        }
        
        
    }];
    
    
    
}

@end
